<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EventiController
 *
 * @author luigi.pastore
 * @created  23-giu-2011
 */
class EventiController extends Classes_Controller
{
    public function init()
    {
	$this->model_live	    = new Classes_Live;
	$this->model_event	    = new Classes_Event;
	$this->model_event_types    = new Classes_EventType;
        $this->model_livestatus     = new Classes_LiveStatus;
        $this->model_province       = new Classes_Province();
        
	$this->event_types = $this->model_event_types->fetchAll();
	
	$this->layout->event_types = $this->event_types;

	// Edit...
	if( is_numeric( $id = $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $event = $this->model_event->fetchById( array( $id ) );

	    if( empty( $event ) )
		trigger_error( 'Nessun Evento' );
	    
	    $this->event = array_shift( $event );
	    
            // Se sto editando precompilo la sessione
            if( !empty( $this->form_data['salva'] ) )
                $this->event = array_merge( $this->event, $this->form_data );
	    
	    // Se sto editando precompilo la sessione
	    if( !empty( $this->event ) AND empty( $_SESSION['Event'] ) )
	    {
		# Formattiamo campi
		// ...

//		$_SESSION['Event']['Info'] = $this->event;
//		$_SESSION['Event']['Live'] = $this->event['event_live'];
	    }	    
	}
        
        # Assegniamo i dati al layout
        $this->layout->data = $this->event;
        $this->layout->event_id = $this->event['event_id'];
	$this->layout->province     = $this->model_province->fetchAll();
        $this->layout->days         = $this->getResource( 'Days' );
        $this->layout->months       = $this->getResource( 'Months' );
        $this->layout->live_status  = $this->model_livestatus->fetchAll();
        
        // Dati Form
        $this->form_data = $this->getParams();
    }

    public function indexAction()
    {
        $this->layout->count_events = $this->model_event->getCount();
	$this->layout->content = array( 'script' => 'eventi/index' );
    }

    public function infoAction()
    {
	if( $this->form_data['conferma'] AND $this->form_data['conferma'] !== '' )
	{
	    $this->layout->data = $this->form_data;

            // Mapping...
            $data = array(
                'event_id'		=> $this->event['event_id'],
                'event_title'		=> $this->form_data['event_title'],
                'event_type'		=> $this->form_data['event_type'],
                'event_desc'		=> $this->form_data['event_desc'],
                'event_notes'		=> $this->form_data['event_notes'],
                'event_published'	=> $this->form_data['event_published']
            );

            // Edit o Aggiunta?
            if( empty( $this->event ) )
                $data['event_createdby'] = $this->application->auth->user['user_id'];
            else
                $data['event_modifiedby'] = $this->application->auth->user['user_id'];

            $model = new Classes_Event( $data );

            if( is_numeric( $event_id = $model->save()->event_id ) )
            {
                // Redirect...
                return header( 'Location: /admin/eventi/live/id/'. $event_id );
            }
            else
            {
                trigger_error( 'Errore Inserimento Evento' );
            }
	}
	
	# Assegniamo i dati al layout
	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'eventi/info' );
    }
    
    public function vediAction()
    {
	if( empty ( $this->event ) )
	    trigger_error( 'Nessun Evento Selezionato' );
	
        if( count( $this->event['live'] ) )
            $this->layout->live_list = $this->model_live->fetchById( array_keys( $this->event['live'] ) );

        $this->layout->days         = $this->getResource( 'Days' );
        $this->layout->months       = $this->getResource( 'Months' );
	$this->layout->content = array( 'script' => 'eventi/vedi' );
    }
    
    public function tourbookAction()
    {
	if( empty ( $this->event ) )
	    trigger_error( 'Nessun Evento Selezionato' );
	
        if( count( $this->event['live'] ) )
            $this->layout->live_list = $this->model_live->fetchById( array_keys( $this->event['live'] ), array( 'orderby' => array( 'live_date' => 'desc' ) ) );

        $this->layout->days         = $this->getResource( 'Days' );
        $this->layout->months       = $this->getResource( 'Months' );
	$this->layout->content = array( 'script' => 'eventi/tourbook' );
    }
    
    public function riepilogoAction()
    {
	if( empty ( $this->event ) )
	    trigger_error( 'Nessun Evento Selezionato' );
        
        $this->model_live       = new Classes_Live;
        $this->model_livestatus = new Classes_LiveStatus;
        $this->model_province   = new Classes_Province();
	
        $this->layout->live_list = array();

        if( count( $this->event['live'] ) )
            $this->layout->live_list = $this->model_live->fetchById( array_keys( $this->event['live'] ) );

        $this->layout->province     = $this->model_province->fetchAll();
        $this->layout->days         = $this->getResource( 'Days' );
        $this->layout->months       = $this->getResource( 'Months' );
        $this->layout->live_status  = $this->model_livestatus->fetchAll();        
	$this->layout->content = array( 'script' => 'eventi/riepilogo' );
    }

    public function liveAction()
    {
        $this->layout->tot = count( (array) $this->event['live'] );
	$this->layout->content = array( 'script' => 'eventi/live' );
    } 
}